package vno.sci.mapEditor
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
	
	import hr.binaria.asx3m.converters.basic.NullConverter;
	
	import org.aswing.ASColor;
	import org.aswing.ASFont;
	
	import vno.fux.BaseDisplayObject;
	import vno.fux.CompassDirection;
	import vno.fux.ExPoint;
	import vno.fux.res.pool.BitmapAttacher;
	import vno.fux.res.pool.NpcResource;
	import vno.fux.res.pool.PNGAttacher;
	import vno.fux.res.pool.ResourceAttacher;
	import vno.fux.res.pool.ResourcePool;
	import vno.sci.Util;
	import vno.sci.window.OLabel;
	import vno.sci.window.ScrollMessageBox;
	import vno.sci.window.SimpleMessageBox;

	
	import vno.fux.res.pool.NpcResource;
	import vno.fux.res.pool.PNGAttacher;
	import vno.fux.res.pool.ResourceAttacher;
	import vno.fux.res.pool.ResourcePool;


	public class BuildingBuoy extends Sprite
	{
		private var _rectWidth:int;
		private var _rectHeight:int;
		
		private var _buoyRadius:int = 8;
		private var _alpha:Number = 0.8;
		
		private var _win:OLabel;//win是加在全局的
		private var _visiblePoints:Vector.<BuildingInfo>;
		private var _winOnShow:Boolean = false;
		
		private var _utilRef:Util;
		
		private var _pivot:BaseDisplayObject;
		private var _image:BitmapAttacher;
		private var _imageVector:Vector.<BaseDisplayObject> = new Vector.<BaseDisplayObject>;
		
		public function BuildingBuoy(ba_:ByteArray, rectWidth_:int, rectHeight_:int)
		{
			_rectWidth = rectWidth_;
			_rectHeight = rectHeight_;
			
			_utilRef = new Util(BuildingInfo.markerToArray(ba_));
			
			_win = new OLabel();
			_win.font = new ASFont("Tahoma", 24, true);
			
			this.addChild(_win);
			
			_image = new BitmapAttacher();
			(ResourcePool.getInstance().chaRes.getRes(999999) as NpcResource).pic.attach(_image);
		}
		
		public function refresh(topLeft:ExPoint, bottomRight:ExPoint):void
		{
			_visiblePoints = BuildingInfo.localMarkerToStage(topLeft.x, topLeft.y, _utilRef.showTest(_rectWidth,_rectHeight,topLeft,bottomRight));
			this.removeChildren();
			myAddChild(_visiblePoints);
		}
		//重设可视区域的Width和Height. 
		public function setRectSizeWH(w:int, h:int):void
		{
			_rectWidth = w;
			_rectHeight = h;
		}
		/**
		 * 加显示标签_win
		 * 然后加各个孩子
		 */		
		private function myAddChild(v:Vector.<BuildingInfo>):void
		{
			this.removeChildren();
			this.addChild(_win);
			for each (var a:BaseDisplayObject in _imageVector)
			{
				a.visible = false;
				a = null;
			}
			_imageVector = new Vector.<BaseDisplayObject>(v.length, true);
			
			for (var i:int = 0; i < v.length; i++)
			{
				var b:Bitmap = new Bitmap(_image.bitmapData);
				//				b.x = v[i].x;
				//				b.y = v[i].y;
				var bdo:BaseDisplayObject = new BaseDisplayObject(b,CompassDirection.CENTER);
				bdo.x = v[i].x;
				bdo.y = v[i].y;
				_imageVector[i] = bdo;
				bdo.alpha = 0.6;
				bdo.addEventListener(MouseEvent.CLICK, onMouseOverShowMessage);
				bdo.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHideMessage);
				this.addChild(bdo);
			}
			
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverShowMessage);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHideMessage);
		}
		
		private function onMouseOutHideMessage(e:MouseEvent):void
		{
			_pivot.alpha = 0.6;
			_pivot.scaleX = 1;
			_pivot.scaleY = 1;
			
			if(_winOnShow)
			{
				_win.visible = false;
			}
		}
		
		private function onMouseOverShowMessage(e:MouseEvent):void
		{
			var a:BaseDisplayObject = e.target as BaseDisplayObject;
			if(a != null)
			{
				_pivot = a;
				a.alpha = 1;
				a.scaleX = 1.25;
				a.scaleY = 1.25;
				
				_win.text = _visiblePoints[_imageVector.indexOf(a)].s;
				_win.x = a.x + 10;
				_win.y = a.y - 30;
				_win.visible = true;
				_winOnShow = true;
			}
		}
		
		
	}
}